import json

from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import pandas as pd

from bqtool.es.es_importcsv import pd_save_es
from bqtool.utils.tools import get_ms_time

es = Elasticsearch(['10.2.111.56'],http_auth=('admin','admin'),port=9200)

def data_save_es(index_name,type_name):
    list = []
    with open(r"E:\EHL\CODE\python\项目\新乡\pr\20181022\41.111.32.20-tvc-2018-10-22-00" ,"r",encoding="utf-8") as lines:
        for line in lines.read().strip().split("\n"):
            items = {}
            if line.startswith("VERSION=1.0"):
                field = line.split("\t")
                for dic in field:
                    value = dic.split("=")
                    if len(value)>0:
                        print(value[0],value[1])
                        items[value[0]] = value[1]
                list.append(items)
    print('ok')

        #     line = eval(line)
        #     # a = line['car_plate_number_prefix']
        #     # print(a)
        #     # 创建action
        #     action = {
        #         "_index":index_name,
        #         "_type":type_name,
        #         "_id":i,
        #         "_source":{
        #             'car_plate_number_prefix': line['car_plate_number_prefix'],
        #             'timestamp': line['timestamp'],
        #             'car_plate_color': line['car_plate_color'],
        #             'tp3': line['tp3'],
        #             'driver_coordinates': line['driver_coordinates'],
        #             'version': line['version'],
        #             'id': line['id'],
        #             'car_color': line['car_color'],
        #             'car_plate_number_index': line['car_plate_number_index'],
        #             'tp2': line['tp2'],
        #             'camera_orientation': line['camera_orientation'],
        #             'travel_orientation': line['travel_orientation'],
        #             'car_plate_number': line['car_plate_number'],
        #             'lane_id': line['lane_id'],
        #             'bay_id': line['bay_id'],
        #             'plate_coordinates': line['plate_coordinates'],
        #             'car_status': line['car_status'],
        #             'tp1': line['tp1'],
        #             'camera_location': line['camera_location'],
        #             'speed': line['speed'],
        #             'car_brand': line['car_brand'],
        #             'car_plate_type': line['car_plate_type']
        #         }
        #         }
        #     i +=1
        #     actions.append(action)
        #     # print(action)
        #     #批量处理
        #     if(len(actions)==500):
        #         success, _ = bulk(es,actions,index=index_name,raise_on_error=True)
        #         count +=1
        #         del actions[0:len(actions)]
        #         print("第{}条成功导入".format(i))
        # if (len(actions)>0):
        #     bulk(es,actions,index=index_name,raise_on_error=True)

def save_es(index_name,type_name):
    reportTime = []
    plateNumber = []
    longitude = []
    latitude = []
    list = []
    with open(r"E:\EHL\CODE\python\项目\道安云\测试数据\test数据\GPS数据.txt" ,"r",encoding="utf-8") as lines:
        for line in lines.readlines():
            data = line.strip().split("@@")
            # reportTime.append()
            # plateNumber.append()
            l = data[4]
            la = data[5]
            # longitude.append()
            # latitude.append()
            list.append([data[0].replace('2017-11-22','2018-12-19'),data[1],float(l)/10**(len(l)-3),float(la)/10**(len(la)-2)])
    df = pd.DataFrame(list,columns=['reportTime','plateNumber','longitude','latitude'])
    df.reportTime = df.reportTime.apply(get_ms_time)
    pd_save_es(df,index_name, type_name,es)
    print('ok')

index_name="komsindex201812"
type_name="koms_car_gps"
save_es(index_name,type_name)
